<?php
/**
@Project: 
@Page: 
@Path: 
@Notes: 
**/
class SHML_Form
{

	protected function create_attributes($a)
	{
		return ($a != "") ? ' ' . str_replace('=','="',implode('" ',explode(",",$a))) . '"' : "";
	}
	
	protected function create_options($a, $d=NULL)
	{
		$o = "";
		foreach ($a as $k => $v) {
			$s = ($k == $d || $v == $d) ? ' selected="selected"' : "";
			$o .= '<option value="' . $k . '"' . $s . '>' . $v . '</option>' . "\n";
		}
		return $o;
	}

/*	public function form_select($properties)
	{
		$attributes = $properties['attributes'];
		$list = $properties['var_list'];
		
		$a = $attributes['attributes'];
		$c = (strtolower($attributes['create']) == "true") ? TRUE : FALSE;
		$d = (isset($attributes['default'])) ? $attributes['default'] : NULL;
		$v = $attributes['value'];
		
		if (isset($list[$v])) {
			$options = $this->create_options($list[$v],$d);
		}
		else {
			$options = "";
		}
		
		$element = ($c) ? '<select'  . $this->create_attributes($a) . '>' . "\n" . $options  . '</select>' . "\n" : $options;
		return $element;
	}*/
	public function form_select($properties) {
		$results = array();
		$value = $properties['attributes']['value'];
		$case = $properties['attributes']['case'];
		if ($value == "") {
			$start = $properties['attributes']['start'];
			$end = $properties['attributes']['end'];
			if ($end < $start) {
				if (!is_numeric($start) && !is_numeric($end)) {
					return "<!--Variable ($start) or Variable ($end) is not defined. Please check the spelling or if it exists.-->";
				}
				else {
					$tmp = $end;
					$end = $start;
					$start = $tmp;
				}
			}
		}
		$default = $properties['attributes']['default'];
		$prefix = ($start != "" && $end != "" && strlen($start) == "2") ? TRUE : FALSE;
		$default = ($default != "") ? $properties['var_list'][$default] : "";
		while ($start <= $end) {
			if ($start < "10") {
				if ($prefix == TRUE && strlen($start !== "2") && $start != "1") {
					array_push($results, '0' . $start);
				}
				else {
					array_push($results, $start);
				}
			}
			else {
				array_push($results, $start);
			}
			$start++;
		}

		$results  = ($tmp != "") ? array_reverse($results) : $results;	
		if ($value != "") {
			$results = $properties['var_list'][$value];
		}	
		foreach ($results as $k=>$v) {
			if ($case == "upper") {
				$v = strtoupper($v);
			}
			if ($case == "lower") {
				$v = strtolower($v);
			}
			if ($case == "ucwords") {
				$v = ucwords($v);
			}
			
/*			if (is_array($default)) {
				foreach ($default as $k1=>$v1) {
					$values .= '<option value="' . $k1 . '" selected>' . $v1 . '</option>';	
				}
			}*/
			$values .= ($v == $default) ? '<option value="' . $default . '" selected>' . $v . '</option>' : '<option value="' . $k . '">' . $v . '</option>';		
		}
		
		return $values;
	}	
}